package org.csu.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.equipment.entity.*;
import org.csu.equipment.persistence.ClientLoginMapper;
import org.csu.equipment.persistence.ClientMapper;
import org.csu.equipment.persistence.EmployeeMapper;
import org.csu.equipment.persistence.TaskMapper;
import org.csu.equipment.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private ClientLoginMapper clientLoginMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Integer login(String CId, String CPwd) {
        QueryWrapper<ClientLogin> queryWrapper = new QueryWrapper<ClientLogin>();
        queryWrapper.eq("C_id",CId).eq("C_pwd",CPwd);
        Integer count = clientLoginMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public void newTask(Client client, String desc,String skill) {
        Integer TId = taskMapper.selectCount(null)+1000;
        Task task = new Task();
        task.setTId(TId.toString());
        task.setTAddress(client.getCAddress());
        task.setCId(client.getCId());
        task.setTStatus("未接单");
        task.setDescription(desc);
        task.setTime(new Timestamp(System.currentTimeMillis()));
        task.setSkill(skill);
        taskMapper.insert(task);
    }

    @Override
    public void updateInfo(Client client) {
        clientMapper.updateById(client);
    }

    @Override
    public void updatePwd(ClientLogin clientLogin) {
        clientLoginMapper.updateById(clientLogin);
    }

    @Override
    public void reminder(String TId) {
        Task task = taskMapper.selectById(TId);
        task.setTStatus("已催单");
        taskMapper.updateById(task);
    }

    @Override
    public Client getClient(String CId) {
        Client client = clientMapper.selectById(CId);
        return client;
    }

    @Override
    public List<Task> getTask(String CId) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        queryWrapper.eq("C_id",CId);
        List<Task> taskList = taskMapper.selectList(queryWrapper);
        return taskList;
    }

    @Override
    public void CommentEmploy(String EId, int credit,String TId) {
        Employee employee = (Employee) employeeMapper.selectById(EId);
        employee.setECredit(employee.getECredit()+credit);
        employeeMapper.updateById(employee);
        Task task = (Task)taskMapper.selectById(TId);
        task.setTStatus("已评价");
        taskMapper.updateById(task);
    }

    @Override
    public boolean isNotG(String TId) {
        Task task = (Task)taskMapper.selectById(TId);
        return task.getTStatus().equals("未接单");
    }
}
